//  ------------ 测试产生虚拟数据 -------------------


//  ------------ 赋值 ------------------------
new Vue({
    el: '#app',
    data: function () {
        return {
            activeName: 'first',
            resourceSearchDialog: false,
            statusList: [{
                label: "保留",
                value: "保留"
            }, {
                label: "已禁用",
                value: "已禁用"
            }, {
                label: "已启用",
                value: "已启用"
            }, {
                label: "备用",
                value: "备用"
            }],
            resourceForm: {
                handle: '',
                plant: '',
                resource: '',
                description: '',
                statusFk: '',
                dateEffect: '',
                dateExpire: ''
            },
            rules: {
                resource: [
                    {required: true, message: '请输入资源名称', trigger: 'blur'}
                ]
            },
            resourceDataDialog: []
        }
    },
    methods: {
        queryResource(){
            var me = this, _form = me.resourceForm;
            var _hiddenHandle = document.getElementById("resourceHandleHidden").value;
            var param = {
                serviceName: "exposeResourceService",
                serviceMethodName: "selectOneResourceByHandle",
                handle: _hiddenHandle
            };
            function callBack(data) {
                if (data.resultCode === "S") {
                    var resultList = data.result;
                    if (resultList.length > 0) {
                        var obj = resultList[0];
                        _form.resource = obj.resource;
                        _form.description = obj.description;
                        _form.statusFk = obj.statusFk;
                        _form.dateEffect = obj.dateEffect;
                        _form.dateExpire = obj.dateExpire;
                        _form.handle = obj.handle;
                    }
                } else {
                    me.$notify.error({
                        title: '查询出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        resourceSearch(){
            var me = this, _form = me.resourceForm;
            var _resource = _form.resource;
            var param = {
                serviceName: "exposeResourceService",
                serviceMethodName: "resourceList",
                plant: "TEST",
                resource: _resource
            };

            function callBack(data) {
                if (data.resultCode === "S") {
                    me.resourceDataDialog = data.result;
                    me.resourceSearchDialog = true;
                } else {
                    me.$notify.error({
                        title: '查询出错！',
                        message: data.resultMsg
                    });
                }
            };
            me.ajaxGetRequest(param, callBack);
        },
        saveResource(formName){
            var me = this;
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    var _form = me.resourceForm;
                    var param = {
                        serviceName: "exposeResourceService",
                        serviceMethodName: "updateOrAddResource",
                        plant: "TEST",
                        resource: _form.resource,
                        description: _form.description,
                        statusFk: _form.statusFk,
                        dateEffect: _form.dateEffect,
                        dateExpire: _form.dateExpire
                    };

                    function callBack(data) {
                        if (data.resultCode === "S") {
                            me.$notify.info({
                                title: '消息',
                                message: '保存成功!'
                            });
                        } else {
                            me.$notify.error({
                                title: '保存出错！',
                                message: data.resultMsg
                            });
                        }
                    };
                    me.ajaxGetRequest(param, callBack);
                } else {
                    console.log('error submit!!');
            return false;
        }
        });
        },
        clearResource(){
            this.resourceForm.handle = '';
            this.resourceForm.plant = '';
            this.resourceForm.resource = '';
            this.resourceForm.description = '';
            this.resourceForm.statusFk = '';
            this.resourceForm.dateEffect = '';
            this.resourceForm.dateExpire = ''
        },
        deleteResource(){
            var me = this;
            var hiddenHandle = document.getElementById("resourceHandleHidden").value;
            if (hiddenHandle === "") {
                this.$notify({
                    title: '警告',
                    message: '删除的唯一标识未获取到,请先执行查询!',
                    type: 'warning'
                });
            } else {
                var param = {
                    serviceName: "exposeResourceService",
                    serviceMethodName: "deleteResource",
                    handle: hiddenHandle
                };

                function callBack(data) {
                    if (data.resultCode === "S") {
                        me.resourceForm = {};
                        me.$notify.info({
                            title: '消息',
                            message: '删除成功!'
                        });
                    } else {
                        me.$notify.error({
                            title: '删除出错！',
                            message: data.resultMsg
                        });
                    }
                };
                me.ajaxGetRequest(param, callBack);
            }
        },
        ajaxPostRequest(params, callBackFun)
        {
            var me = this, _url = "http://localhost:8083/route";
            axios({
                method: 'post',
                url: _url,
                params: params
            }).then(function (response) {
                if (response.status == 200) {
                    callBackFun(response.data);
                } else {
                    console.log(response);
                }
            }).catch(function (err) {
                console.log(err);
            });
        },
        ajaxGetRequest(params, callBackFun){
            var me = this, _url = "http://localhost:8083/route";
            axios.get(_url, {
                params: params
            }).then(function (response) {
                if (response.status == 200) {
                    callBackFun(response.data);
                } else {
                    console.log(response);
                }
            }).catch(function (err) {
                console.log(err);
            });
        },
        selectedRowResource(row, event) {
            var handle = row.handle;
            var resource = row.resource;
            this.resourceSearchDialog = false;
            this.resourceForm.resource = resource;
            document.getElementById("resourceHandleHidden").value = handle;
        }
    }
})